package com.zhanvie.thread;

import com.zhanvie.singleton.RunState;
import com.zhanvie.config.TaskConfig;
import com.zhanvie.signal.KillSignalHandler;
import com.zhanvie.singleton.QueueManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class Main1 {

    private static final Logger logger = LogManager.getLogger(Main1.class);

    public static void main(String[] args) {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        String progress = runtimeMXBean.getName();
        long pid = Long.parseLong(progress.split("@")[0]);
        logger.info("program start pid: {}", pid);

        KillSignalHandler handler = new KillSignalHandler();
        handler.registerSignal("INT");
        handler.registerSignal("TERM");

        RunState.getInstance().start();
        RunState.getInstance().setPid(pid);

        cyclicBarrierTestThread();
    }

    private static void cyclicBarrierTestThread() {
        ExtendThreadPool poolExecutor = new ExtendThreadPool(5, 5,
                QueueManager.getInstance().getThreadQueue(),
                new CustomThreadFactory("calc"),
                new ExtendRejectHandler(50));
        int cyclicBarrierTestThreadCount = TaskConfig.cyclicBarrierTestThreadCount;
        for (int i = 0; i < cyclicBarrierTestThreadCount; i++) {
            poolExecutor.submit(new CyclicBarrierThread());
        }
        poolExecutor.shutdown();
    }


}
